<?php
/**
 * Created by PhpStorm.
 * User: jokejie
 * Date: 2021/12/3
 * Time: 21:30
 */

namespace backend\controllers;


use common\models\Admin;
use common\services\AdminRoleService;
use common\models\OperLog;
use Yii;
use yii\helpers\ArrayHelper;

class OperLogController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = OperLog::class;
    }

    public function actionIndex()
    {
        $where = [];
        if (Yii::$app->request->isPost) {
            $return = parent::actionIndex(true, $where);
            $list = $return['data'];
            $auid = array_unique(ArrayHelper::getColumn($list,'auid'));
            $admin = Admin::find()->where(['id'=>$auid])->select(['realname','username','id'])->asArray()->all();
            $admin = array_column($admin,null,'id');
            foreach ($list as $key => $val) {
                $roleNameStr = '';
                $roleList = AdminRoleService::getListByAdmin($val['auid']);
                if ($roleList) {
                    $roleNameArr = ArrayHelper::getColumn($roleList, 'name');
                    $roleNameStr = implode(',', $roleNameArr);
                }
                $val['rolename'] = $roleNameStr;
                $val['username'] = $admin[$val['auid']]['username']??'';
                $list[$key] = $val;
            }
            $return['data'] = $list;
            return $return;
        } else {
            return parent::actionIndex();
        }
    }

    public function actionDetail($id)
    {
        $data = $this->model::find()->where(['id'=>$id])->one();
        $roleList = AdminRoleService::getListByAdmin($data->auid);
        $roleNameStr = '';
        if ($roleList) {
            $roleNameArr = ArrayHelper::getColumn($roleList, 'name');
            $roleNameStr = implode(',', $roleNameArr);
        }
        $admin = Admin::find()->where(['id'=>$data->auid])->select(['username'])->scalar();
        return $this->render('',['operLog'=>$data,'roleNameStr'=>$roleNameStr,'admin'=>$admin]);
    }
}